<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!--<el-form-item label="网关id" prop="gatewayId">
        <el-input
          v-model="queryParams.gatewayId"
          placeholder="请输入网关id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="父设备id" prop="parentId">
        <el-input
          v-model="queryParams.parentId"
          placeholder="请输入父设备id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="设备名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备ip" prop="ip">
        <el-input
          v-model="queryParams.ip"
          placeholder="请输入设备ip"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="设备端口" prop="port">
        <el-input
          v-model="queryParams.port"
          placeholder="请输入设备端口"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入设备用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备用户密码" prop="password">
        <el-input
          v-model="queryParams.password"
          placeholder="请输入设备用户密码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备厂家" prop="factory">
        <el-input
          v-model="queryParams.factory"
          placeholder="请输入设备厂家"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="信令协议类型" prop="protocol">
        <el-input
          v-model="queryParams.protocol"
          placeholder="请输入信令协议类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备经度" prop="longitude">
        <el-input
          v-model="queryParams.longitude"
          placeholder="请输入设备经度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备纬度" prop="latitude">
        <el-input
          v-model="queryParams.latitude"
          placeholder="请输入设备纬度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入设备地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="在线状态" prop="online">
        <!--<el-input
          v-model="queryParams.online"
          placeholder="请输入在线状态 0离线1在线"
          clearable
          @keyup.enter.native="handleQuery"
        />-->
        <el-select v-model="queryParams.online" placeholder="请选择在线状态" clearable>
          <el-option
            v-for="dict in dict.type.vplat_device_online"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="激活状态 0正常 1停用" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择激活状态 0正常 1停用" clearable>
          <el-option
            v-for="dict in dict.type.sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['vplat:device:add']"
        >新增</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-notebook-2"
          size="mini"
          @click="handleBind"
          v-hasPermi="['vplat:device:bind']"
        >挂载</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['vplat:device:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['vplat:device:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['vplat:device:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备编码" align="center" prop="id" />
      <!--<el-table-column label="网关id" align="center" prop="gatewayId" />
      <el-table-column label="父设备id" align="center" prop="parentId" />-->
      <el-table-column label="设备名称" align="center" prop="name" />
      <el-table-column label="设备ip" align="center" prop="ip" />
      <el-table-column label="设备端口" align="center" prop="port" />
      <el-table-column label="设备用户名" align="center" prop="username" />
      <el-table-column label="设备用户密码" align="center" prop="password" />
      <el-table-column label="设备厂家" align="center" prop="factory" >
		  <template slot-scope="scope">
          	<dict-tag :options="dict.type.vplat_device_factory" :value="scope.row.factory"/>
          </template>
	  </el-table-column>
      <el-table-column label="设备类型" align="center" prop="type" />
      <el-table-column label="信令协议" align="center" prop="protocol" >
		  <template slot-scope="scope">
          	<dict-tag :options="dict.type.vplat_device_protocol" :value="scope.row.protocol"/>
          </template>
	  </el-table-column>
      <!--<el-table-column label="设备经度" align="center" prop="longitude" />
      <el-table-column label="设备纬度" align="center" prop="latitude" />
      <el-table-column label="设备地址" align="center" prop="address" />-->
      <el-table-column label="在线状态" align="center" prop="online" >
		  <template slot-scope="scope">
          	<dict-tag :options="dict.type.vplat_device_online" :value="scope.row.online"/>
          </template>
	  </el-table-column>
      <!--<el-table-column label="激活状态 0正常 1停用" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />-->
      
      <!--<el-table-column label="创建时间" align="center" prop="createTime" >
		  <template slot-scope="scope">
          	<span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
	  </el-table-column>-->
	  
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['vplat:device:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['vplat:device:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
		<el-row :gutter="20" type="flex" justify="space-between" align="middle">
			<el-col :span="12">
				<el-form-item label="设备编码" prop="id">
		          <el-input v-model="form.id" placeholder="" :disabled="isOnlyRead"/>
		        </el-form-item>
			</el-col>
			<el-col :span="12">
				<el-form-item label="网关id" prop="gatewayId" >
		          <el-input v-model="form.gatewayId" placeholder="请输入网关id" />
		        </el-form-item>
			</el-col>
		</el-row>
		<el-row :gutter="20" type="flex" justify="space-between" align="middle">
			<el-col :span="12">
				<el-form-item label="设备名称" prop="name" >
		          <el-input v-model="form.name" placeholder="请输入设备名称" />
		        </el-form-item>
			</el-col>
			<el-col :span="12">
				<el-form-item label="设备ip" prop="ip" >
		          <el-input v-model="form.ip" placeholder="请输入设备ip" />
		        </el-form-item>
			</el-col>
		</el-row>
		<el-row :gutter="20" type="flex" justify="space-between" align="middle">
			<el-col :span="12">
				<el-form-item label="设备端口" prop="port" >
		          <el-input v-model="form.port" placeholder="请输入设备端口" />
		        </el-form-item>
			</el-col>
			<el-col :span="12">
				<el-form-item label="设备用户名" prop="username" >
		          <el-input v-model="form.username" placeholder="请输入设备用户名" />
		        </el-form-item>
			</el-col>
		</el-row>
		<el-row :gutter="20" type="flex" justify="space-between" align="middle">
			<el-col :span="12">
				<el-form-item label="设备密码" prop="password" >
		          <el-input v-model="form.password" placeholder="请输入设备密码" />
		        </el-form-item>
			</el-col>
			<el-col :span="12">
				<el-form-item label="设备厂家" prop="factory" >
		          <el-select v-model="form.factory" placeholder="请选择设备厂家" style="width:100%;">
		            <el-option
		              v-for="dict in dict.type.vplat_device_factory"
		              :key="dict.value"
		              :label="dict.label"
		              :value="dict.value"
		            ></el-option>
		          </el-select>
		        </el-form-item>
			</el-col>
		</el-row>
		<el-row :gutter="20" type="flex" justify="space-between" align="middle">
			<el-col :span="12">
				<el-form-item label="信令协议" prop="protocol" >
		          <el-select v-model="form.protocol" placeholder="请选择信令协议" style="width:100%;">
		            <el-option v-if="filter(dict)"
		              v-for="dict in dict.type.vplat_device_protocol"
		              :key="dict.value"
		              :label="dict.label"
		              :value="dict.value"
		            ></el-option>
		          </el-select>
		        </el-form-item>
			</el-col>
			<el-col :span="12">
				<el-form-item label="设备类型" prop="type" >
		          <el-input v-model="form.type" placeholder="请输入设备类型" />
		        </el-form-item>
			</el-col>
		</el-row>
		<el-row :gutter="20" type="flex" justify="space-between" align="middle">
			<el-col :span="12">
				<el-form-item label="设备经度" prop="longitude" >
		          <el-input v-model="form.longitude" placeholder="请输入设备经度" />
		        </el-form-item>
			</el-col>
			<el-col :span="12">
				<el-form-item label="设备纬度" prop="latitude" >
		          <el-input v-model="form.latitude" placeholder="请输入设备纬度" />
		        </el-form-item>
			</el-col>
		</el-row>
		<el-row :gutter="20" type="flex" justify="space-between" align="middle">
			<el-col :span="12">
				<el-form-item label="在线状态" prop="online">
		          <el-select v-model="form.online" placeholder="请选择在线状态" style="width:100%;">
		            <el-option
		              v-for="dict in dict.type.vplat_device_online"
		              :key="dict.value"
		              :label="dict.label"
		              :value="parseInt(dict.value)"
		            ></el-option>
		          </el-select>
		        </el-form-item>
			</el-col>
		</el-row>
		<el-row :gutter="20" type="flex" justify="space-between" align="middle">
			<el-col :span="24">
				<el-form-item label="设备地址" prop="address">
		          <el-input v-model="form.address" placeholder="请输入设备地址" />
		        </el-form-item>
			</el-col>
		</el-row>
       
        <!--<el-form-item label="父设备id" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入父设备id" />
        </el-form-item>-->

        <!--<el-form-item label="激活状态 0正常 1停用" prop="status">
          <el-select v-model="form.status" placeholder="请选择激活状态 0正常 1停用">
            <el-option
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>-->

        <!--<el-form-item label="数据状态" prop="delFlag">
          <el-select v-model="form.delFlag" placeholder="请选择数据状态">
            <el-option
              v-for="dict in dict.type.sys_oper_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>-->
        <!--
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        -->
      </el-form>
      
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<style scoped>

</style>

<script>
import { listDevice, getDevice, delDevice, addDevice, updateDevice } from "@/api/vplat/device";

export default {
  name: "Device",
  dicts: ['sys_oper_type', 'sys_normal_disable', 'vplat_device_online', 'vplat_device_factory','vplat_device_protocol'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否仅读
      isOnlyRead: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        gatewayId: null,
        parentId: null,
        name: null,
        ip: null,
        port: null,
        username: null,
        password: null,
        factory: null,
        type: null,
        protocol: null,
        longitude: null,
        latitude: null,
        address: null,
        online: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        gatewayId: [
          { required: true, message: "网关id不能不能为空", trigger: "blur" }
        ],
        ip: [
          { required: true, message: "设备ip不能不能为空", trigger: "blur" }
        ],
        port: [
          { required: true, message: "设备端口不能不能为空", trigger: "blur" }
        ],
        protocol: [
          { required: true, message: "信令协议不能不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    /*
    this.dict.type['online']=[
	    {
			label:"离线",
			value:"0",
			raw: {listClass: "default", cssClass: ""},
		},
	    {
			label:"在线",
			value:"1",
			raw: {listClass: "default", cssClass: ""},
		},
	];
	console.log(this.dict.type.online);
	*/
  },
  methods: {
    /** 过滤协议 */
    filter(dict) {
      if(this.title == '添加设备' && dict.value == '2'){
		  return false;
	  }
	  return true;
    },
    /** 查询设备列表 */
    getList() {
      this.loading = true;
      listDevice(this.queryParams).then(response => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        gatewayId: null,
        parentId: null,
        name: null,
        ip: null,
        port: null,
        username: null,
        password: null,
        factory: null,
        type: null,
        protocol: null,
        longitude: null,
        latitude: null,
        address: null,
        online: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备";
    },
    /** 挂载按钮操作 */
    handleBind() {
      this.$router.push('/vplat/device/bind');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDevice(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDevice(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDevice(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除设备编号为"' + ids + '"的数据项？').then(function() {
        return delDevice(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('vplat/device/export', {
        ...this.queryParams
      }, `device_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
